package com.example.yrdev.ordering_system.Model;

import android.content.Context;
import android.database.Cursor;

import com.example.yrdev.ordering_system.Database.DingHanDB;

import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by luke on 21/11/2016.
 */

public class Operator {
    public static   final String DH_TABLE_NAME = "OperatorList";

    public static   final String operator_authority="operator_authority";
    public static   final String operator_employeeID="operator_employeeID";
    public static  final String operator_operCardNo="operator_operCardNo";
    public static  final String operator_password="operator_password";


    public  static final String DH_CREATE_TABLE="CREATE TABLE " + DH_TABLE_NAME + "( " +operator_employeeID + " TEXT," +operator_operCardNo + " TEXT,"
            + operator_password + " TEXT," + operator_authority + " TEXT"+  ")";




    private String authority;
    private String employeeID;
    private String operCardNo;
    private String password;
    private static  Operator operator;

    public static Operator setInstance(Operator op){
        if (operator==null){
            operator=op;


        }

        return operator;

    }
    public static  Operator getIntance(){

        return operator;

    }
    public static void saveOperator(Context context, Operator operator){

        DingHanDB.getInstance(context).insert(DH_TABLE_NAME,operator);

    }

    public static ArrayList<Operator> getOperatorList(Context context){

        return DingHanDB.getInstance(context).getOperatorList();

    }
    public Operator(){

    }
    public static void deleteAllData(Context context){

        DingHanDB.getInstance(context).deleteAllData(DH_TABLE_NAME);
    }


    public Operator(JSONObject jsonObject){

        try {

            this.authority=jsonObject.getString( "authority");

            this.employeeID=jsonObject.getString("employeeID");

            this.operCardNo=jsonObject.getString("operCardNo");

            this.password=jsonObject.getString("password");

        }catch (Exception e){

        }


    }

    public Operator(Cursor cursor){

        try {

            this.authority=cursor.getString(cursor.getColumnIndex(operator_authority));


            this.employeeID=cursor.getString(cursor.getColumnIndex(operator_employeeID));

            this.operCardNo=cursor.getString(cursor.getColumnIndex(operator_operCardNo));

            this.password=cursor.getString(cursor.getColumnIndex(operator_password));

        }catch (Exception e){

            e.printStackTrace();

        }


    }

    public String getAuthority() {
        return authority;
    }

    public String getPassword() {
        return password;
    }

    public String getEmployeeID() {
        return employeeID;
    }

    public String getOperCardNo() {
        return operCardNo;
    }

    private boolean getAuthorityAtIndex(int index){
        String val=getAuthority().charAt(index)+"";
        if (val.equals("1")){
            return true;

        }else {
            return false;

        }
    }
    public Boolean returnCommodityAuthority(){//退款


        return getAuthorityAtIndex(0);

    }

    public Boolean updateCardyAuthority(){//退款
        return getAuthorityAtIndex(0);
    }
    public Boolean issueCardyAuthority(){//退款

        return getAuthorityAtIndex(1);
    }
    public Boolean rechargeAuthority(){//退款

        return getAuthorityAtIndex(2);
    }
    public Boolean refundAuthority(){//退款

        return getAuthorityAtIndex(3);
    }
    public Boolean usbAuthority(){//退款

        return getAuthorityAtIndex(4);
    }
    public Boolean cashAuthority(){//退款

        return getAuthorityAtIndex(5);
    }
    public Boolean initAuthority(){//退款

        return getAuthorityAtIndex(6);
    }
    public Boolean memberCardAuthority(){//退款

        return getAuthorityAtIndex(7);
    }
    public Boolean reportAuthority(){//退款

        return getAuthorityAtIndex(8);
    }
    public Boolean refillAuthority(){//退款

        return getAuthorityAtIndex(9);
    }

    public Boolean settingAuthority(){//退款

        return getAuthorityAtIndex(10);
    }
}
